//
//  EarnDailyView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/10/6.
//

import UIKit

class EarnDailyView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 18)
        label.textColor = .black
        label.text = "DAILY TASKS"
        return label
    }()
    
    var taskClosure: AnyClosure<TaskModel>?
    
    var dailyTaskArr = [TaskModel]() {
        didSet {
            isHidden = dailyTaskArr.count == 0
            for item in subviews {
                if item.isKind(of: EarnDailyTaskView.self) {
                    item.removeFromSuperview()
                }
            }
            if dailyTaskArr.count > 0 {
                for (i, v) in dailyTaskArr.enumerated() {
                    let view = EarnDailyTaskView()
                    addSubview(view)
                    view.snp.makeConstraints { make in
                        make.top.equalTo(42+i*77)
                        make.left.right.equalTo(0)
                        make.height.equalTo(77)
                    }
                    view.model = v
                    view.taskClosure = { [weak self] m in
                        self?.taskClosure?(m)
                    }
                }
            }
            layoutIfNeeded()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(12)
            make.height.equalTo(20)
        }
        wn_cornerWithBorder(8, 0.1, .white, 1, .init(white: 0, alpha: 0.1), .zero, 8, 1)
        layer.masksToBounds = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class EarnDailyTaskView: UIView {
    
    lazy var taskName: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .init(named: "black_333333")
        return label
    }()
    
    lazy var taskDes: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_666666")
        return label
    }()
    
    lazy var goldImage: UIImageView = {
        return UIImageView(image: UIImage(named: "reward_coin"))
    }()
    
    lazy var goldLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "red_FFB8C7")
        return label
    }()
    
    lazy var taskBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        btn.wn_cornerWithBorder(16)
        btn.rx.tap.bind { [weak self] in
            self?.handleBtn()
        }.disposed(by: rx.disposeBag)
        btn.isEnabled = false
        return btn
    }()
    
    var taskClosure: AnyClosure<TaskModel>?
    
    var model: TaskModel? {
        didSet {
            if let m = model {
                taskName.text = m.taskName
                taskDes.text = m.desc
                goldLabel.text = "+\(m.gold)"
                if m.status == 1 {
                    taskBtn.isEnabled = true
                    taskBtn.setTitle("Go", for: .normal)
                    taskBtn.backgroundColor = .init(named: "red_FFB8C7")
                }else if m.status == 2 {
                    taskBtn.isEnabled = true
                    taskBtn.setTitle("Claim", for: .normal)
                    taskBtn.backgroundColor = .init(named: "red_FE6390")
                }else if m.status == 3 {
                    taskBtn.isEnabled = false
                    taskBtn.setTitle("Done", for: .normal)
                    taskBtn.backgroundColor = .init(named: "gray_E0E8EE")
                }
            }
        }
    }
    
    func handleBtn() {
        if let m = model {
            if m.status == 2 {
                taskClosure?(m)
            }else {
                if let url = m.jumpUrl {
                    switch url {
                    case "store":
                        viewContainingController?.wn_pushStoreVC()
                    case "library":
                        viewContainingController?.navigationController?.popToRootViewController(animated: true)
                        wn_getTopVC()?.tabBarController?.tabBar.isHidden = false
                        WNThread.wn_delay(0.1) {
                            wn_getTopVC()?.tabBarController?.selectedIndex = 0
                        }
                    case "recommend":
                        viewContainingController?.navigationController?.popToRootViewController(animated: true)
                        wn_getTopVC()?.tabBarController?.tabBar.isHidden = false
                        WNThread.wn_delay(0.1) {
                            wn_getTopVC()?.tabBarController?.selectedIndex = 1
                        }
                    case "userInfo":
                        viewContainingController?.navigationController?.pushViewController(WNUserInfoVC(), animated: true)
                    case "facebook":
                        viewContainingController?.wn_presentLogin()
                    default: break
                    }
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(taskName)
        addSubview(taskDes)
        addSubview(goldImage)
        addSubview(goldLabel)
        addSubview(taskBtn)
        taskName.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(12)
            make.right.equalTo(-94)
        }
        taskDes.snp.makeConstraints { make in
            make.top.equalTo(taskName.snp.bottom).offset(6)
            make.left.equalTo(taskName.snp.left)
            make.right.equalTo(-94)
        }
        goldImage.snp.makeConstraints { (make) in
            make.top.equalTo(taskDes.snp.bottom).offset(6)
            make.left.equalTo(taskDes.snp.left)
            make.width.height.equalTo(16)
        }
        goldLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(goldImage.snp.centerY)
            make.left.equalTo(goldImage.snp.right).offset(6)
        }
        taskBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.size.equalTo(CGSize.init(width: 70, height: 32))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
